<template>
    <div>
      <form-head @onSubmit="onSubmit" :is_change="is_change"/>
      <el-form ref="form" :rules="rules" :model="form" label-width="130px" class="layout-all" :label-position="isMobile()?'top':'left'">
        <el-form-item label="记录人员:" prop="card_name">
          <el-input v-model="form.card_name" placeholder="请输入记录人员" class="form-width-50" />
        </el-form-item>
      
        <el-form-item label="商机时间:" prop="subscribe_time">
          <el-date-picker
            v-model="form.subscribe_time"
            type="date"
            placeholder="选择未取预警"
            class="form-width-10"
            @change="dateChange()"
          />
        </el-form-item>
        <el-form-item label="提醒时间:" prop="send_time">
          <el-date-picker
            v-model="form.send_time"
            type="date"
            placeholder="选择提醒时间"
            class="form-width-10"
            @change="dateChange1()"
          />
        </el-form-item>
        <el-form-item label="会员手机号:" prop="mobile">
          <el-input v-model="form.mobile" class="form-width-10" placeholder="请输入会员手机号" />
        </el-form-item>

        <el-form-item label="商机详情">
            <el-input type="textarea" v-model="form.details_text"></el-input>
        </el-form-item>
      
       
        <el-form-item label="沟通记录:" prop="text">
          <rich-text ref="myQuillEditor" :content="form.text" class="form-width-50" @change="is_change=true"/>
        </el-form-item>


      </el-form>
    
    </div>
</template>

<script>
import richText from '@/components/rich_text/rich_text'
import videoManager from "@/components/videoManager/videoManager";
import UploadBanners from "@/components/UploadPic/UploadBanners";
// import cutImageDialog  from "@/components/UploadPic/UploadPic";
export default {
  name:"GoodsForm",
  computed: {
       model(){
         return this.$store.state.user.model;
       }
    },
  components: { richText, videoManager,UploadBanners },
  data() {
    return {
      logistics_isshow:false,
      video_name:'',
      pro_type:1,
      is_change: undefined,
       lianji_options: [
     
        ],
      spec_list: [],
      list_thumb: [],
      integral_name: "积分",
      integral_scale: 1,
      price_commission: 0,
      id: 0,
      selected_options: [],
      options: [],
      options2: [],
      props_shop: {
        value: 'id',
        label: 'title',
        children: 'sub',
      },
      level_info: [],
      level_info_list: [],
      level_min: '',
      level_max: '',
      shop_options: [],
      thumb: '',
      RETAIL_LEVEL: 0,
      proportion: {
        commission_type:0,
        type:0,
        	fixed:[],
        	proportion:[],
      },
      level_discount: {
        commission_type:0,
        	fixed:[],
        	proportion:[],
      },
      form: {
        video_id:'',
        jilian_text:'0',
        id: 0,
        shop_id: 0,
        cate_t1_id: 1,
        cate_t2_id: '',
        cate_t3_id: '',
        shop_cate_t1_id: '',
        goods_sn:'',
        shop_cate_t2_id: '',
        subscribe_time: '',
        send_time:'',
        status: true,
        is_hot: false,
        is_new: false,
        is_recommend: false,
        is_choiceness: false,
        sort: 1,
        title: '',
        thumb: '',
        banners: [],
        goods_append: {is_prescription:false},
        level_info: [],
        description: '',
        goods_amount: '',
        total_amount: '',
        distri_price: 0.00,
        remark: '',
        details_text: '',
        pv: 100,
        sale_inv: 0,
        stock: 100,
        is_commission: false,
        is_level_discount: false,
        group_is_commission: false,
        special_is_commission: false,
        seckill_is_commission: false,
        is_not_level_dis: false,
        integral_buy: 0,
        integral_give: 0,
        cost_price: 0,
        swap_amount: 0,
        express_amount: 0,
        keywords: '',
      },
      rules: {
        title: [
          { required: true, message: '请输入商品名', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    // this.id = 7
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
 
    // 未取预警
    dateChange() {
      const date = new Date(this.form.subscribe_time)
      const Y = date.getFullYear() + ''
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      this.form.subscribe_time = Y + '-' + M + '-' + D
    },
    dateChange1(){
      const date = new Date(this.form.send_time)
      const Y = date.getFullYear() + ''
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1)
      const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
      this.form.send_time = Y + '-' + M + '-' + D
    },
    handleSubCat(value,id) {
        // 获取value值
        var lianji_options = this.lianji_options
        var value_text = '';
        for(var j = 0;j<value.length;j++){
          if(j+1 == value.length){
            value_text+=value[j];
          }else{
            value_text+=value[j]+"/";
          }
        }
        for(var i=0;i<lianji_options.length;i++){
          if(lianji_options[i]['id'] == id){
              var text = lianji_options[i]['text'];
              lianji_options[i]['text'] = text+value_text
          }
        }

        console.log(lianji_options);
        
    },
    update_thumb(e,i){
      if(i<0){
        this.form.banners.push(e)
      }else{
        if(e==''){
          this.$delete(this.form.banners,i)
        }else{
          this.$set(this.form.banners,i,e)
        }
        
      }
    },
    del_select(){
      this.form.video_id = 0
      this.video_name = ''
    },
  
    // 提交
    onSubmit(formName) {
      const form = this.form
      form.text = this.$refs.myQuillEditor.editor_content
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.type = 2

          //form.proportion.commission_type	=form.proportion.commission_type=== true ?1:0
          this.get('/'+this.model+'/Registration/save', form, 'POST').then(e => {
            this.$msg_ok('success', '操作成功')
            this.$back()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      let data={
        id:this.id,
      }
      this.get('/'+this.model+'/Registration/info',data, 'POST').then(e => {

          this.lianji_options = e.jilian_option
      

        // this.level_info_list.unshift({id:0,title:'无限制'})
        if(this.id>0){
          this.form = e.info
        this.form.jilian_text = e.info.product_specification
           this.$refs.myQuillEditor.editor_content = this.form.text
        }else{
          
        }
        
        //分佣数组
        this.RETAIL_LEVEL = e.RETAIL_LEVEL
        if(e.proportion!=null){
          this.proportion=e.proportion
          //this.proportion.commission_type	=this.proportion.commission_type== 1 ?true:false
        }
        this.proportion=this.setCommArray(this.proportion,this.proportion.type==0?this.RETAIL_LEVEL:this.level_info.length)

        //等级折扣
        if(e.level_discount!=null){
          this.level_discount=e.level_discount
        }
        this.level_discount=this.setCommArray(this.level_discount,this.level_info.length)

       
       setTimeout(()=>{this.is_change=false},100) 
      })
    },
    //转换数组长度
    setCommArray(arr,length){
      let count=length-arr.fixed.length
      if(count<0){
        arr.fixed=arr.fixed.slice(0,length)
      }else if(count>0){
        for(let i=0 ;i<count;i++){
          arr.fixed.push(0)
        }
      }
      count=length-arr.proportion.length
      if(count<0){
        arr.proportion=arr.proportion.slice(0,length)
      }else if(count>0){
        for(let i=0 ;i<count;i++){
          arr.proportion.push(0)
        }
      }
      return arr
    },
    // 商品分类赋值
    handleShopCate(val) {
      this.form.shop_cate_t1_id = val[0]
      this.form.shop_cate_t2_id = val[1]
    },
    //
    onEditorReady(editor) {
    },
    // 添加类型
    add_columnName() {
      this.spec_list.push({
        id: 0,
        title: '',
        sort: 1,
        item: [{ id: 0, title: '', sort: 0 }]
      })
    },
    // 删除规格
    del_columnName(index) {
      this.spec_list.splice(index, 1)
    },
    // 添加属性
    add_attribute(index) {
      this.spec_list[index].item.push({ id: 0, title: '', sort: 0 })
    },
    // 删除属性
    del_attribute(index, i) {
      if (this.spec_list[index].item.length > 1) {
        this.spec_list[index].item.splice(i, 1)
        return false
      }
      this.$msg_confirm('该规格只有一条属性,如果删除,规格也会删除,是否删除?').then(e => {
        this.spec_list.splice(index, 1)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
          this.is_change=true
      },
      deep:true
    },
    level_discount:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
          this.is_change=true
      },
      deep:true
    },
    proportion:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
          this.is_change=true
      },
      deep:true
    },
  }
}
</script>

<style >


@media (max-width:550px) {
  .form-80{
    width: 100% !important;

  }
  .form-30{
    width: 40% !important;
  }
}
.form-30{
    width: 30% ;
  }
.layout-all {
  margin: 20px;
}
.spces_div{
  border:1px solid #DCDFE6;
  height: 100%;
  width: 30%;
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
}
.avatar-uploader {
  width:110px;
  height:110px;
}
.avatar-uploader .el-upload--picture-card {
  width:110px;
  height:110px;
}
.avatar_item img{
  width:110px;
  height:110px;
}
</style>
